/**
 * put it in session,it contains the AjaxTag for user
 */
package com.dcivision.framework.taglib.channel;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

import com.dcivision.framework.ApplicationException;


public class AjaxTagFactory {

  
  /**
   * if the AjaxTag relate to conn,It scope should be request.
   * because conn will be close after one request.............
   * so this factory can not be static,after one request,it should be desposed
   *
   * conn contained in pageContext,at Method: getAjaxTag will set pageContext again...
   * so above question will not exists again.
   */
  private Map containerMap= new HashMap();
  private static AjaxTagFactory instance = new AjaxTagFactory();
  
  private AjaxTagFactory(){
    
  }
  
  public static AjaxTagFactory getInstance(){
    return instance;
  }
  
  public  Object getAjaxTag(PageContext pageContext,String tagName) throws ApplicationException{
    TagSupport tag = null;
    
    String className = getWholeTagName(tagName);
    
    AjaxTagContainer container = (AjaxTagContainer)containerMap.get(tagName);
    
    if(container==null){
      container=new AjaxTagContainer(className);
      containerMap.put(tagName, container);
    }
    
    tag = container.getTag();
    
    if(tag!=null){
      tag.setPageContext(pageContext);
    }
    
    return tag;
  }
  
  public void releaseTag(String tagName,TagSupport tag) throws ApplicationException{
    
    String className = getWholeTagName(tagName);
    
    AjaxTagContainer container = new AjaxTagContainer(className);
    
    if(tag!=null){
      tag.setPageContext(null);
      container.putTag(tag);//put to container pool
    }
    
  }
  
  private String getWholeTagName(String tagName) throws ApplicationException{
    String className = null;
    if(AjaxConstant.workflowPriorityTag.equalsIgnoreCase(tagName)) {
      className = AjaxWorkflowPriorityTag.class.getName();
   
    } else if(AjaxConstant.workflowStatusFlagTag.equalsIgnoreCase(tagName)) {
      className = AjaxStatusFlagTag.class.getName();
     
    } else if(AjaxConstant.workflowStepInfoTag.equalsIgnoreCase(tagName)) {
      className = AjaxWorkflowStepInfoTag.class.getName();
      
    } else if(AjaxConstant.relateObjectInfoTag.equalsIgnoreCase(tagName)) {
      className = AjaxRelateObjectInfoTag.class.getName();
      
    } else if(AjaxConstant.workflowDudateTag.equalsIgnoreCase(tagName)) {
      className = AjaxWorkflowDudateTag.class.getName();
     
    } else if(AjaxConstant.workflowFromWhomInfoTag.equalsIgnoreCase(tagName)) {
      className = AjaxFromWhomInfoTag.class.getName();
      
    } else if(AjaxConstant.workflowActionTakerTag.equalsIgnoreCase(tagName)) {
      className = AjaxWorkflowActionTakerTag.class.getName();
      
    } else if(AjaxConstant.documentInfoTag.equalsIgnoreCase(tagName)) {
      className = AjaxDocumentInfoTag.class.getName();
      
    } else if(AjaxConstant.dmsLocationPathTag.equalsIgnoreCase(tagName)) {
      className = AjaxDmsLocationPathTag.class.getName();
      
    } else if(AjaxConstant.durationTag.equalsIgnoreCase(tagName)) {
      className = AjaxDurationTag.class.getName();
      
    } else if(AjaxConstant.summarizeContentTag.equalsIgnoreCase(tagName)) {
      className = AjaxSummarizeContentTag.class.getName();
      
    } else if(AjaxConstant.DMSCONTEXTMENUTAG.equalsIgnoreCase(tagName)) {
      className = AjaxDMSContextMenuTag.class.getName();
      
    } else{
      throw new ApplicationException("Did not find the Tag");
    }
    return className;
  }
}
